package com.zkingsoft.async;


import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.matrix.async.core.TaskHandler;
import com.matrix.core.tools.DateUtil;
import com.matrix.core.tools.LogUtil;
import com.zkingsoft.bean.BizBusiness;
import com.zkingsoft.bean.BizOrder;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.bean.DataDictionary;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.dao.BizBusinessDao;
import com.zkingsoft.dao.BizUserDao;
import com.zkingsoft.dao.DataDictionaryDao;
import com.zkingsoft.service.BizOrderService;

public class VipOrderHandler implements TaskHandler {
	private static final long serialVersionUID = -2719945289184242205L;

	@Autowired
	private BizOrderService bizOrderService;
	@Autowired
	private BizBusinessDao businessDao;
	@Autowired
	private BizUserDao bizUserDao;
	@Autowired
	DataDictionaryDao dataDictionaryDao;
	
	@Override
	public boolean execute(Map<String, String> dataMap) {
		
		String orderId = dataMap.get("orderId");
		String orderNo = dataMap.get("orderNo");
		String payMoney = dataMap.get("payMoney");
		BizOrder order= bizOrderService.findById(orderId); 
		if(AppConstance.PAY_STATUS_SUC==order.getPayStatus()){
			DataDictionary vipType= dataDictionaryDao.selectById(order.getOrderRemark());
			//更新用户的到期时间
			String openId=order.getUserId();
			BizUser user= bizUserDao.findByOpenId(openId);
			BizBusiness business=	businessDao.findByUserOpenId(openId);
			
			Date vipEmdTime=new Date();
			if(DateUtil.isAffterDate(business.getVipEndTime(), vipEmdTime)){
					business.setVipEndTime(vipEmdTime);
			} 
			//设置到期时间为设定的时间
			business.setVipEndTime(
					DateUtil.getDateAfter(business.getVipEndTime(),
					Integer.parseInt(vipType.getParentId())));
			//设置vip标识
			business.setIsVip(1);
			businessDao.updateByModel(business);
			LogUtil.debug("设置用户{}为vip，到期时间为{}", user.getNickName(),business.getVipEndTime().toLocaleString());
			
		}else{
			LogUtil.info("订单不是支付成功状态不处理order status={}", order.getOrderStatus());
		}
		return true;
	}

	 

	@Override
	public void onError(Map<String, String> dataMap, Exception e) {

	}

}
